home *** CD-ROM | disk | FTP | other *** search
- /* EnumerateHFSCatalog.c */
- /*
- * List In A List Sample
- * EnumerateHFSCatalog.c
- * Copyright © 1993-94 Apple Computer Inc.
- *
- * EnumerateHFSCatalog is a recursive function that scans an HFS file directory,
- * building a list of the file and folder names in a hierarchical list. It is
- * interesting only as an example of how to use the hierarchical list functions.
- */
- #include "ListInAList.h"
- #include <Files.h>
-
- /*
- * EnumerateHFSCatalog
- * Enumerate the HFS at this level. To enumerate a volume, clear the hierarchical
- * list and call EnumerateHFSCatalog with the following parameters:
- * theList the hierarchical list
- * volumeRefNum volume to catalog
- * volumeDirIndex fsRtDirID: the root directory
- * indentLevel zero
- * Each call to EnumerateHFSCatalog builds a sub-list. When the function finds a
- * folder, it calls itself recursively with the folder's volumeDirIndex and an
- * incremented indentLevel. EnumerateHFSCatalog returns the index of the first
- * element (i.e., the first file or folder in the directory it enumerated). This
- * is stored in the subElement of the parent list.
- *
- * This is not a good algorithm design as it will evaluate the entire disk before
- * processing events. This means that the user cannot interrupt the application and
- * that other parts of the system will not run. Since the Macintosh requires
- * "cooperative multitasking," a production program should be organized so that
- * this evaluation takes place during idle time in the event loop. To minimize
- * this problem (for debugging), we call Button each time through the loop and
- * stop if the user clicks on the mouse.
- */
- TwistDownHdl
- EnumerateHFSCatalog(
- ListHandle theList,
- short indentLevel,
- short volumeRefNum,
- long volumeDirIndex
- )
- {
- HFileInfo pb;
- Str255 fileName;
- OSErr status;
- short dirIndex;
- TwistDownHdl previousElement;
- TwistDownHdl thisElement;
- TwistDownHdl firstElement;
- EventRecord currentEvent;
- Str255 windowTitle;
- Str15 depthString;
- #define ELEM (**thisElement)
- #define FLAG (ELEM.flag)
-
- /*
- * Fiddle the window title to show progress.
- */
- pstrcpy(windowTitle, "\pFolder depth: ");
- NumToString(indentLevel, (StringPtr) &depthString);
- pstrcat(windowTitle, depthString);
- pstrcat(windowTitle, "\p - Click to stop");
- SetWTitle(FrontWindow(), windowTitle);
- /* */
- Clear(pb);
- pb.ioNamePtr = fileName;
- pb.ioVRefNum = volumeRefNum;
- dirIndex = 1;
- firstElement = NULL;
- previousElement = NULL;
- /*
- * This loop scans the volume for all directories and files. The loop
- * exits on error (i.e., no more files) or when the user clicks on
- * the mouse.
- */
- do {
- /*
- * Call EventAvail every time through this loop so that background
- * programs get time to get some work done.
- */
- EventAvail(everyEvent, ¤tEvent);
- SpinCursor();
- pb.ioFDirIndex = dirIndex;
- pb.ioDirID = volumeDirIndex;
- status = PBGetCatInfo((CInfoPBPtr) &pb, SYNC);
- if (status == noErr) {
- status = MakeTwistDownElement(
- previousElement,
- indentLevel,
- fileName[0],
- (Ptr) &fileName[1],
- &thisElement
- );
- }
- if (status == noErr) {
- if (firstElement == NULL)
- firstElement = thisElement;
- /*
- * If this is a folder, descend the hierarchy by calling ourselves
- * recursively. Store the first element in the new sub-list
- * into our subElement pointer. Note that FLAG.hasTwistDown will
- * be TRUE even if there are no files in the sub-folder.
- */
- if ((pb.ioFlAttrib & ioDirMask) == ioDirMask) {
- ELEM.flag |= kHasTwistDown;
- ELEM.subElement = EnumerateHFSCatalog(
- theList,
- indentLevel + 1,
- volumeRefNum,
- pb.ioDirID
- );
- }
- ++dirIndex;
- previousElement = thisElement; /* Set linkage for next file */
- }
- } while (status == noErr && Button() == FALSE);
- return (firstElement);
- }
-
-